package Thread.New.线程组.Expection_Deal_With;


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MyThreadGroup extends ThreadGroup{
    private FileWriter fw;

    public MyThreadGroup(String name) {
        super(name);
        try {
            fw = new FileWriter("C:\\Users\\啦啦啦\\Desktop\\bug.txt",true);
        } catch (IOException e) {
            System.out.println("创建出现异常");
        }
    }

    public MyThreadGroup(ThreadGroup parent, String name) {
        super(parent, name);
        try {
            fw = new FileWriter("C:\\Users\\啦啦啦\\Desktop\\bug.txt",true);
        } catch (IOException e) {
            System.out.println("创建出现异常");
        }
    }

    /*继承ThreadGroup类,重写uncaughtException方法,用来处理抛出的异常*/


    @Override
    public void uncaughtException(Thread t,Throwable e) {
        try {
            fw.write("线程: "+t.getName()+" 引发了异常\r\n");
            fw.write("异常原因:"+e.getMessage()+"\r\n");
            fw.write("--------------------------------\r\n");
            fw.flush();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }
}
